if(typeof(currents_awards)=='undefined') var currents_awards = [10,20,30];//当前金银铜总数，结束时自动加1
if(typeof(sendUrl)=='undefined')var sendUrl="http://act.itapit.net/Act/Hot/rioAwards";
var wordJson = {
			"word": [{
				"cn": "里约",
				"en": "Rio"
			}, {
				"cn": "奥林匹克",
				"en": "Olympic"
			}, {
				"cn": "中国",
				"en": "China"
			}, {
				"cn": "胜利",
				"en": "Win"
			}],
			"total": "4"
		}
		var medalType=0;//默认是金牌0 银牌1  铜牌2
		var wordList = []; //存储点击原始词
		var wordAdd = ["轻", "logo", "敲"]; //存储当词不够的时候增补的词或者图片
		var wordDisplay = []; //存储随机排列后词
		var wordPush = "" //记录输入的正确的字
		var num = 0 //存储当前做的题数
		var txtNum = 0; //记录比对的顺序
		var goState = 0; //0 不跳转  1  跳转到下一题
		var time = 50; //初始计时
		var curtime = 0; //过程计时
		var lasttime = 0; //实际做题所花时间
		var watch=0;//获取秒
		var interval, reg = /^\d$/,
            sleep = 10,
            sum = 0;
		
		var ti ;//计时器
		var wat;
		var domImg = '<img src="../images/28.png"/>'; //logo地址

		
		function init() {
			
			$("div.gameWap li").each(function(index) {
					$(this).on("click",function() {
						var txt = $(this).text();
						compare(txt, index);
					})
				}) //绑定点击事件

			$("a.startOpr").on("click",function(){
				$("div.gameStart").hide();
				$("div.gameIng").show();
				ti = setInterval("timeStart()", 1000);
				wat=setInterval("getTime()", 10);
			})
			
			$("a.gameOver_renovate").on("click",function(){
				reStart();
			})
			
			getWord(num);

		}
		function reStart(){
			clearInterval(ti);
			clearInterval(wat);
			
			wordList = []; //存储点击原始词
			wordDisplay = []; //存储随机排列后词
			wordPush = "" //记录输入的正确的字
			num = 0 //存储当前做的题数
			txtNum = 0; //记录比对的顺序
			goState = 0; //0 不跳转  1  跳转到下一题
			time = 30; //初始计时
			curtime = 0; //过程计时
			lasttime = 0; //实际做题所花时间
			watch=0;//计时精确到毫秒
			getWord(num);
			$("div.gameOver,div.gameIng").hide();
			$("div.gameStart").show();
			$("#glodTypeJs").html("");
		}
		function timeStart() {
			curtime++;
			lasttime++
			if(curtime >= time) {
				curtime = time;
			}
			var per = Math.round(curtime/time * 100) + '%';
			$("div.timeBar").width(per);
		}
		function getTime(){
			sum++;
            var d = new Date("1111/1/1,0:0:0");
            d.setSeconds(sum);
            var h = d.getHours();
            h = reg.test(h) ? "0" + h + "." : h + "."
            var m = d.getMinutes();
            m = reg.test(m) ? "0" + m + "." : m + "."
            var s = d.getSeconds();
            s = reg.test(s) ? "0" + s : s;
			watch=lasttime+'.'+s;
		
		}
		
		function compare(t, index) { //比较点击的词

			if(t == wordJson.word[num].en[txtNum]) {
				
				$("div.gameWap li").eq(index).addClass("right").siblings().removeClass("right").removeClass("wrong")
				wordPush += t;
				$("p.enWord").text(wordPush).css({"opacity":"1"});
				txtNum++;
			} else {
				
				$("div.gameWap li").eq(index).addClass("wrong").siblings().removeClass("right").removeClass("wrong")
			}
			if(txtNum == wordJson.word[num].en.length) {
				txtNum = 0;
				wordPush = "";
				$("p.enWord").text(wordPush).css({"opacity":"0"});
				next();
			}
			
		}

		function randomsort(a, b) {
			return Math.random() > .5 ? -1 : 1;
		} //重新随机排列数组
		
		function getWord(n) {
			wordList = [];
			var len = wordJson.word[n].en.length;
			$("p.cnWord").text(wordJson.word[n].cn)
			for(var i = 0; i < len; i++) {
				wordList.push(wordJson.word[n].en[i]);
				//$("p.cnWord").append("<em>"+wordJson.word[i].cn+"</em>");
			}
			if(len < 9) {
				var addNum = 9 - len;
				for(var j = 0; j < addNum; j++) { //增加临时填补词
					var w = wordAdd[Math.round(Math.random() * 2)];
					wordList.push(w);
				}

			}
			wordDisplay = wordList.sort(randomsort);
			
			showWord(wordDisplay);
		}

		function showWord(show) {
			for(var i = 0; i < show.length; i++) {
				if(show[i] == "logo") {
					$("div.gameWap li").eq(i).append(domImg);
				} else if(show[i] == "轻" || show[i] == "敲") {
					$("div.gameWap li").eq(i).text(show[i]);
				} else {
					$("div.gameWap li").eq(i).text(show[i]);
				}
			}
		}

		function next() {
			$("div.gameWap li").each(function() {
				$(this).text("").removeClass("right").removeClass("wrong");
			})
			
			if(num==wordJson.total-1){
				gameOver();
			}else{
				num++;
				getWord(num);
			}
			
			
		}
		function gameOver(){
			clearInterval(ti);
			clearInterval(wat);
			$("div.gameIng").hide();
			$(".lastTime").text(watch);
			if(lasttime<=15){
				var glob=currents_awards[0]+1+"<em>块金牌!</em>";
				$("#glodTypeJs").append(glob);
				$(".gameOver_medal img").attr("src","../images/gameOver_gold.png");
				medalType=0;
			}else{
				if(lasttime<=20){
					var glob=currents_awards[1]+1+"<em>块银牌!<em>";
					$("#glodTypeJs").append(glob);
					$(".gameOver_medal img").attr("src","../images/gameOver_silver.png");
					medalType=1;
				}else{
					var glob=currents_awards[2]+1+"<em>块铜牌!</em>";
					$("#glodTypeJs").append(glob);
					$(".gameOver_medal img").attr("src","../images/gameOver_bronze.png");
					medalType=2;
				}
			}
			$("div.gameOver").show();
			sendMedal();
			var txt='助力奥运中国队，我用'+watch+'秒获得英语填词项目金牌！';
			$("#pageTitle").text(txt)
		}
		function sendMedal(){
			$.get(sendUrl, {type:medalType+1, count:"1",sec:watch } );
			//var txt='挑战'+lasttime+'秒，轻敲一下支持中国奥运夺金牌1枚';
			//$("#pageTitle").text(txt)
//			var data={ type:medalType, count: "1" }
//			$.ajax({
//				type:"get",
//				url:sendUrl,
//				data:data,
//				async:true
//			});
		}
		init();